﻿// Inner Fire 游戏引擎库
// DxUI 渲染器UI功能
//
// Copyright (c) 2025 by 尔西大帝. All rights reserved.
//
// 用于DirectX的渲染器。
// 此文件分装用于UI功能
//
// Author: 尔西大帝
// Email: 2670613871@qq.com
// Created: 2025-04-24

#include "dx_ui.h"
#include "dx_graphics.h"
#include <Windows.h>

namespace ifire::dx {
DxUI::DxUI(DxGraphics* dx) : DxModule(dx) {}

void DxUI::Init() {
  // 用于UI的深度图。它不需要与其它的项目共用层叠
  ui_depth_map_ = std::make_unique<UIDepthMap>(
      dx_->Device(), dx_->GetWidth(), dx_->GetHeight());

  // 用于UI文字
  UIMeshDesc text_desc = {};
  text_desc.pDevice = dx_->Device();
  text_desc.pCmdList = dx_->CommandList();
  text_desc.ClientSize = XMFLOAT2(static_cast<float>(dx_->GetWidth()),
      static_cast<float>(dx_->GetHeight()));
  text_desc.Capacity = dx_->GetFrameRes().GetTextCount();

  // 用于UI图片
  UIMeshDesc img_desc = text_desc;
  text_desc.Capacity = dx_->GetFrameRes().GetUICount();

  // 用于Canvas
  UICanvasDesc canvas_desc = {};
  canvas_desc.Widget = dx_->GetClientWidget();

  uimesh_ = std::make_unique<UIMeshManager>(text_desc, img_desc, canvas_desc);
}

void DxUI::OnResize() {
  if (uimesh_ != nullptr) {
    auto& viewport = dx_->GetView().GetScreenViewport();
    auto new_size = XMFLOAT2(viewport.Width, viewport.Height);
    uimesh_->OnResize(dx_->GetClientWidget());
  }
}


} // namespace ifire::dx